
var config = require('./config')
// ===================== My kit ================
var CONST = {
  CHILD_TAG:function CHILD_TAG(){},
  VDOM:function VDOM(){},
  RENDERED:function RENDERED(){},
  VTEXT:function VTEXT(){},
  NOTHING:function NOTHING(){},
  REPLACE:function REPLACE(){},
  PROPS_REMOVE:function PROPS_REMOVE(){},
  PROPS_REPLACE:function PROPS_REPLACE(){},
  REMOVE:function REMOVE(){},
  APPEND:function APPEND(){},
  PATCH:function PATCH(){},
  CHILD:function CHILD(){},
  MYMSG:function MYMSG(){},
  MYMODELMSG:function MYMODELMSG(){},
  ISARRAYHTML:function ISARRAYHTML(){}
}

function isMsg(a){
  return a&&a.ctype == CONST.MYMSG
}
function isArrMap(a){
  return a&&a.type == CONST.ArrMap
}
function isFunction(a){
  return typeof a === 'function'
}
function isArray(a){
  return Object.prototype.toString.call(a) == '[object Array]'
}
function isVDOM(a){
  return a==null?false
  : a.type === CONST.VDOM
}
function isVTEXT(a){
  return a==null?false
  : a.type === CONST.VTEXT
}
function isRendered(vdom){
  return vdom&&vdom.type2 === CONST.RENDERED
}
function isNothing(a){
  return a === CONST.NOTHING
}
function isObject(a){
  return Object.prototype.toString.call(a) === '[object Object]'
}
function isString(a){
  return Object.prototype.toString.call(a) ==='[object String]'
}
function isNumber(a){
  return Object.prototype.toString.call(a) ==='[object Number]'
}
function hasChild(a){
  return a&&a.child&&a.child.length&&a.child.length > 0
}
function isBoolean(a){
  return Object.prototype.toString.call(a) ==='[object Boolean]'
}
function isPromise(p){
  return Object.prototype.toString.call(p) ==='[object Promise]'
}
function log(){
  if(config.log){
    window.console&&console.log.apply(console,[].slice.call(arguments))
  }else{
    void null
  }
}
function time(a){
  if(config.log){
    window.console&&console.time(a)
  }else{
    void null
  }
}
function timeEnd(a){
  if(config.log){
    window.console&&console.timeEnd(a)
  }else{
    void null
  }
}
// div.a.b#abc -> {name:'div',className:'a b',id:'abc'}
function parserTag(token){
  var name = null
  var id = null
  var classNames = []

  var stus = [{
    type:'tag',
    c:[]
  }]
  for(var i=0;i<token.length;i++){
    if(token.charAt(i) == '.'){
      setparam(stus[stus.length-1])
      stus.push({
        type: 'class',
        c:[]
      })
    }else if(token.charAt(i) == '#'){
      setparam(stus[stus.length-1])
      stus.push({
        type:'id',
        c:[]
      })
    }else{
      stus[stus.length-1].c.push(token.charAt(i))
    }
  }
  setparam(stus[stus.length-1])

  function setparam(item){
    if(item.type == 'tag'){
      var tokenname = item.c.join('')
      name = tokenname||'div'
    }else if(item.type == 'class'){
      classNames.push(item.c.join(''))
    }else{
      id = item.c.join('')
    }
  }
  return {
    name:name,
    className:classNames.length?classNames.join(' '):null,
    id:id
  }
}

module.exports = {
  CONST:CONST,
  isFunction:isFunction,
  isArray:isArray,
  isVDOM:isVDOM,
  isVTEXT:isVTEXT,
  isRendered:isRendered,
  isNothing:isNothing,
  isObject:isObject,
  isString:isString,
  isNumber:isNumber,
  hasChild:hasChild,
  isBoolean:isBoolean,
  isPromise:isPromise,
  log:log,
  time:time,
  timeEnd:timeEnd,
  parserTag:parserTag,
  isMsg:isMsg
}